package com.ai.chatbackend.service.ai;


import com.ai.chatbackend.mapper.ai.ChatServiceMapper;
import com.ai.chatbackend.pojo.po.SpringAiChatMemory;
import com.ai.chatbackend.pojo.vo.AiChatHistoryVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatServiceImpl extends ServiceImpl<ChatServiceMapper, SpringAiChatMemory> implements ChatService {

    @Autowired
    private ChatServiceMapper chatServiceMapper;

    @Override
    public List<AiChatHistoryVO> getHistory(String chatId) {
        QueryWrapper<SpringAiChatMemory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("conversation_id",chatId);
        List<SpringAiChatMemory> memoryList = chatServiceMapper.selectList(queryWrapper);
        List<AiChatHistoryVO> historyList = memoryList.stream().map(
             item->{
                 AiChatHistoryVO historyVO = new AiChatHistoryVO();
                 historyVO.setRole(item.getType());
                 historyVO.setContent(item.getContent());
                 historyVO.setChatId(item.getConversationId());
                 return historyVO;
             }
        ).toList();
        return historyList;
    }
}
